package com.jianzh5.action.front;

import com.jianzh5.action.BaseAction;
import com.jianzh5.entity.*;
import com.jianzh5.entity.enums.NewsTypeEnum;
import com.jianzh5.page.PageView;
import com.jianzh5.pojo.PCase;
import com.jianzh5.pojo.PCategory;
import com.jianzh5.pojo.PGame;
import com.jianzh5.pojo.PProduct;
import com.jianzh5.service.*;
import com.jianzh5.util.InfoCenterHome;
import com.jianzh5.util.IpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by jianzh5 on 2015/8/3.
 */

/**
 * 游戏首页控制器
 */
@Controller
public class IndexAction extends BaseAction {
    @Autowired
    private StaticServiceI staticService;

    @Autowired
    private AskServiceI askService;

    //类别服务器
    @Autowired
    private CategoryServiceI categoryService;
    //产品服务器
    @Autowired
    private ProductServiceI productService;
    @Autowired
    private CaseServiceI caseService;
    @Autowired
    private NewsServiceI newsService;


    /**
     * 首页页面
     * @param model
     * @return
     */
    @RequestMapping(value = {"","index"})
    public String index(Model model){
        List<PProduct> productList = productService.getProductList(0,16,"createDate desc");
        model.addAttribute("productList", productList);
        List<PCategory> categories = categoryService.cateList();
        model.addAttribute("categories",categories);
        List<TCase> caseList = caseService.getCaseList(0,8,"createDate desc");
        model.addAttribute("caseList",caseList);
        List<TNews> newsList = newsService.getNewsList(0,4,"createDate desc");
        model.addAttribute("newsList",newsList);

        return InfoCenterHome.SERVLET_FRONT_ROOT+"index";
    }

    /**
     * 关于我们页面
     * @return
     */
    @RequestMapping("about")
    public String about(Model model){
        int aboutType = 1;
        TStatic tStatic = staticService.getStaticByType(aboutType);
        String aboutUs = tStatic.getContext();
        model.addAttribute("about",aboutUs);
        return InfoCenterHome.SERVLET_FRONT_ROOT+"about";
    }

    /**
     * 联系我们页面
     * @return
     */
    @RequestMapping("contact")
    public String contact(Model model){
        int aboutType = 2;
        TStatic tStatic = staticService.getStaticByType(aboutType);
        String contactUs = tStatic.getContext();
        model.addAttribute("contact",contactUs);
        return InfoCenterHome.SERVLET_FRONT_ROOT+"contact";
    }

    /**
     * 客户留言页面
     * @return
     */
    @RequestMapping(value = "ask",method = RequestMethod.GET)
    public String ask(){
        return InfoCenterHome.SERVLET_FRONT_ROOT+"ask";
    }

    /**
     * 提交客户留言
     * @param tAsk
     * @param request
     * @return
     */
    @RequestMapping(value = "ask",method = RequestMethod.POST)
    public String ask(TAsk tAsk,HttpServletRequest request,RedirectAttributes red){
        String message = "";
        try{
            String ip = IpUtils.getIpAddr(request);
            tAsk.setIp(ip);
            askService.editAsk(tAsk);
            message = "我们已收到您的留言,将会第一时间与您联系!";
        }catch (Exception e){
            logger.error("插入留言异常:",e);
            message = "系统异常:"+e.getMessage();
        }
        red.addFlashAttribute("message",message);
        return "redirect:ask";
    }

    /**
     * 新闻页面
     * @return
     */
    @RequestMapping("news")
    public String news(PageView<TNews> page,Model model,Integer type,HttpServletRequest request){
        model.addAttribute("newsType", NewsTypeEnum.newsTypeEnumMap);
        String uri = request.getRequestURI();
        page.setSize(12);
        page = newsService.newsList(page,type,"createDate desc");
        model.addAttribute("page",page);
        if(type != null){
            uri = uri+"?type="+type;
        }
        model.addAttribute("uri",uri);
        return InfoCenterHome.SERVLET_FRONT_ROOT+"news";
    }

    /**
     * 新闻详细页面
     * @param id
     * @return
     */
    @RequestMapping("new/detail/{id}")
    public String newDetail(@PathVariable int id,Model model){
        TNews tNews = newsService.getNews(id);
        model.addAttribute("currentNew",tNews);
        TNews preNews = newsService.getNews(id-1);
        TNews nextNews = newsService.getNews(id+1);
        model.addAttribute("preNew",preNews);
        model.addAttribute("nextNew",nextNews);
        model.addAttribute("newsType", NewsTypeEnum.newsTypeEnumMap);
        return InfoCenterHome.SERVLET_FRONT_ROOT+"newDetail";
    }

    /**
     * 产品展示
     * @return
     */
    @RequestMapping("product")
    public String product(PageView<PProduct> page,Integer cid,Model model,HttpServletRequest request){
        String uri = request.getRequestURI();
        page.setSize(12);
        page = productService.productList(page,cid,"createDate desc");
        model.addAttribute("page",page);
        List<PCategory> categories = categoryService.cateList();
        model.addAttribute("categories",categories);
        if(cid != null){
            PCategory category = categoryService.getPCategory(cid);
            model.addAttribute("category",category);
            uri = uri+"?cid="+cid;
        }
        model.addAttribute("uri",uri);
        return InfoCenterHome.SERVLET_FRONT_ROOT+"product";
    }

    /**
     * 产品详细页面
     * @return
     */
    @RequestMapping("product/detail/{id}")
    public String productDetail(@PathVariable int id,Model model){
        PProduct currentProduct = productService.getProduct(id);
        model.addAttribute("currentProduct",currentProduct);
        PProduct preProduct = productService.getProduct(id-1);
        PProduct nextProduct = productService.getProduct(id+1);
        model.addAttribute("preProduct",preProduct);
        model.addAttribute("nextProduct",nextProduct);
        List<PCategory> categories = categoryService.cateList();
        model.addAttribute("categories",categories);
        return InfoCenterHome.SERVLET_FRONT_ROOT+"productDetail";
    }


    /**
     * 案例展示
     * @return
     */
    @RequestMapping("cases")
    public String cases(PageView<TCase> page,Model model){
        page.setSize(12);
        page = caseService.CaseList(page,"createDate desc");
        model.addAttribute("page",page);
        return InfoCenterHome.SERVLET_FRONT_ROOT+"cases";
    }

    /**
     * 案例详细页面
     * @return
     */
    @RequestMapping("case-detail/{id}")
    public String caseDetail(@PathVariable int id,Model model){
        PCase currentCase = caseService.getPCase(id);
        model.addAttribute("currentCase",currentCase);
        PCase preCase = caseService.getPCase(id-1);
        PCase nextCase = caseService.getPCase(id+1);
        model.addAttribute("preCase",preCase);
        model.addAttribute("nextCase",nextCase);
        return InfoCenterHome.SERVLET_FRONT_ROOT+"caseDetail";
    }

}
